<template>
  <div class="app">
    <el-dialog
      :visible="visible"
      :destroy-on-close="true"
      :title="form.id ? '编辑权限点' : '添加权限点'"
      width="510px"
      @close="cancel"
    >
      <el-form ref="form" :model="form" label-width="auto" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="introduce">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="form.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="warning" round @click="confirm">确定</el-button>
          <el-button type="info" round @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  permissionDetails,
  getPermissionListAPI,
  addPermissionAPI,
  editPermissionAPI
} from '@/api/permission'
export default {
  name: 'SmallDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    permissionId: {
      type: [Number, String],
      default: 0
    },
    tableData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              const { data } = await permissionDetails(this.permissionId)
              const list = await getPermissionListAPI()
              const childrenData = list.data.filter(
                (item) => item.id !== data.id
              )
              if (childrenData.some((item) => item.name === value)) {
                callback(new Error('权限名称不能重复'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              const { data } = await getPermissionListAPI()
              if (data.some((item) => item.enVisible === value)) {
                callback(new Error('权限标识不能重复'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 确定
    async confirm() {
      await this.$refs.form.validate()
      if (this.form.id) {
        await editPermissionAPI(this.form)
      } else {
        this.form.pid = parseInt(this.permissionId)
        this.form.type = 2
        await addPermissionAPI(this.form)
      }
      this.$emit('tableList')
      this.$emit('update:visible', false)
      this.form.id && delete this.form.id
    },
    // 编辑权限点数据
    async editPermissionData(id) {
      const { data } = await permissionDetails(id)
      this.form = data
    },
    // 取消
    cancel() {
      this.$refs.form.resetFields()
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style>
</style>
